package com.czk.client;

import com.czk.future.SyncWriteMap;
import com.czk.future.WriteFuture;
import com.czk.msg.Response;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

/**
 * @Author:ChenZhangKun
 * @Date: 2021/4/27 11:34
 */
public class MyClientHandler extends ChannelInboundHandlerAdapter {
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        System.out.println("接收到了" +
                "");
        //
        if (msg instanceof Response) {
            // 强转
            Response response = (Response) msg;
            // 拿到请求id
            String requestId = response.getRequestId();
            // 拿到缓存
            WriteFuture future = SyncWriteMap.syncKey.get(requestId);
            if (future != null) {
                System.out.println("唤醒");
                future.setResponse(response);
            }
        }
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
        cause.printStackTrace();
        ctx.close();
    }
}
